package cn.nange.cmfz.realm;

import cn.nange.cmfz.dao.AdminDao;
import cn.nange.cmfz.entity.Admin;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;

public class AuthenRealm extends AuthenticatingRealm {

    @Autowired
    private AdminDao adminDao;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//        令牌中获取用户名
       UsernamePasswordToken usernamePasswordToken =  (UsernamePasswordToken) authenticationToken;

        String username = usernamePasswordToken.getUsername();

//        通过 DAO 查询数据库，获取数据库数据

        Admin admin = adminDao.selectByName(username);

//        对象不为 NULL，封装 INFO

        if (admin != null){
            return new SimpleAuthenticationInfo(admin.getName(),admin.getPwd(),this.getName());
        }
        return null;
    }
}
